package org.neurone.userspace.userinfo.impl;

import java.util.HashMap;
import java.util.List;

import org.neurone.application.ApplicationFace;
import org.neurone.application.impl.ApplicationFaceLocalFactory;
import org.neurone.domain.Application;
import org.neurone.domain.Message;
import org.neurone.domain.UserInfo;
import org.neurone.domain.UserConfig;
import org.neurone.domain.UserDetailInfo;
import org.neurone.domain.alter.UserInfoAlter;
import org.neurone.domain.persistence.UserConfigPersistence;
import org.neurone.domain.persistence.UserDetailInfoPersistence;
import org.neurone.domain.persistence.UserInfoPersistence;
import org.neurone.userspace.dao.ApplicationDao;
import org.neurone.userspace.dao.MessageDao;
import org.neurone.userspace.dao.UserDetailDao;
import org.neurone.userspace.dao.UserInfoDao;
import org.neurone.userspace.dao.impl.DaoServerImpl;
import org.neurone.userspace.userinfo.MessageMgr;
import org.neurone.userspace.userinfo.UserFace;
import org.neurone.userspace.userinfo.UserMgr;

public class UserFaceLocal extends UserInfoAlter implements UserFace {

	protected UserInfoDao userInfoDao;
	protected UserDetailDao userDetailDao;
	protected boolean persistentAvailable;
	protected MessageMgr messageMgr;
	
	protected UserFaceLocal()
	{		
		
	}	
	
	
	public MessageMgr getMessageMgr() {
		return messageMgr;
	}


	public void setMessageMgr(MessageMgr messageMgr) {
		this.messageMgr = messageMgr;
	}

	protected UserInfoDao getUserInfoDao() {
		return userInfoDao;
	}

	protected UserDetailDao getUserDetailDao() {
		return userDetailDao;
	}

	protected void setUserInfoDao(UserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}

	protected void setUserDetailDao(UserDetailDao userDetailDao) {
		this.userDetailDao = userDetailDao;
	}

	
	private void initUserInfo(UserInfo userInfo)
	{
		this.persistentAvailable = userInfo != null;
		
		if(this.persistentAvailable)
		{
			this.userID = userInfo.getUserID();
			this.setEmail(userInfo.getEmail());
			this.setImage(userInfo.getImage());
			this.setPasswd(userInfo.getPasswd());
			this.setRealName(userInfo.getRealName());
			this.setStatus(userInfo.getStatus());
			this.setUserName(userInfo.getUserName());
			this.setUserNick(userInfo.getUserNick());

			this.userConfig = userInfo.getUserConfig();
		}
	}
	
	@Override
	public void addDetail(UserDetailInfo userDetail) {
		userDetailDao.addDetail(this.getUserID(),userDetail);
	}

	@Override
	public void deleteDetail(Long detailID) {
		userDetailDao.deleteDetail(detailID);
	}

	@Override
	public void freeze() {
		setStatus("已冻结");
		flush();
	}

	@Override
	public void updateDetail(UserDetailInfo userDetail) {
		this.userDetailDao.update(userDetail);
	}

	@Override
	public void unfreeze() {
		setStatus("正常");
		flush();
	}

	@Override
	public UserConfig getUserConfig() {
		return this.userConfig;
	}

	@Override
	public List<UserDetailInfo> getUserDetailInfoList() {
		return userDetailDao.getUserDetail(this.getUserID());
	}

	@Override
	public void flush() {
		userInfoDao.update(this);
	}

	@Override
	public void updateUserConfig(
			UserConfig userConfig) {
		userInfoDao.updateConfig(this.userID, userConfig);
		this.userConfig = userConfig;
	}

	@Override
	public boolean isPersistentAvailable() {
		return this.persistentAvailable;
	}


	protected void initByUserID(Long userID) {
		initUserInfo(this.userInfoDao.find(userID));
	}


	@Override
	public List<Message> getPersonalAll(int pageSize, int pageIndex) {
		return messageMgr.getPersonalAll(this.userName, pageSize, pageIndex);
	}


	@Override
	public List<Message> getPersonalNews(int pageSize, int pageIndex) {
		return messageMgr.getPersonalNews(this.userName, pageSize, pageIndex);
	}


	@Override
	public List<Message> getPersonalScrip(int pageSize, int pageIndex) {
		return messageMgr.getPersonalScrip(this.userName, pageSize, pageIndex);
	}


	@Override
	public List<Message> getPersonalTask(int pageSize, int pageIndex) {
		return messageMgr.getPersonalTask(this.userName, pageSize, pageIndex);
	}

	@Override
	public void assignApplication(Long applicationID) {
		this.userInfoDao.assignApplication(this.userID, applicationID);
	}


	@Override
	public List<Application> listAssignedApplication() {
		return this.userInfoDao.listAssignedApplication(this.userID);
	}


	@Override
	public void unAssignApplication(Long applicationID) {
		this.userInfoDao.unassignApplication(this.userID, applicationID);
	}
}
